#---- Load libraries and funtions -----

library(tidyverse)
library(haven)
library(scales)
library(lme4)
library(lmerTest)
library(effects)
library(optimx)
library(emmeans)
library(multcomp)
library(multcompView)
library(ggeffects)
library(reghelper)
library(utils)
library(sjPlot)
library(sjstats)
library(sjmisc)
library(ggplot2)
library(jtools)
library(lattice)
library(pwt)
library(dplyr)
library(arm)
library(performance)
library(modelsummary)
library(jmv)
library(margins)

options(scipen=999)# non-scientific notation

# scale between 0-1#-----------------------------------

scale01 <- function(x){(x-min(x, na.rm = T))/(max(x, na.rm = T)-min(x, na.rm = T))} 

#reading data#--------------------------------------------

data <- as.data.frame(read_sav('data_res_interviewer.sav'))

#scaling and centering variables#--------------------------------------------
data$age = scale01(data$age)
data$age_c = scale(data$age,center=TRUE,scale=FALSE)
data$exp = scale01(data$exp)
data$exp = scale(data$exp,center=TRUE,scale=FALSE)
data$edu_cat_f = as.factor(data$edu_cat)
data$settlement_f = as.factor(data$settlement)
data$edu_k_bin = as.factor(data$edu_k_bin)
data$P11_k_sc = scale01(data$P11_k)
data$P11_k_c = scale(data$P11_k_sc,center=TRUE,scale=FALSE)
data$P12_k_sc = scale01(data$P12_k)
data$P12_k_c = scale(data$P12_k_sc,center=TRUE,scale=FALSE)
data$age_k = scale01(data$age_k)
data$age_k_c = scale(data$age_k,center=TRUE,scale=FALSE)

#running multilevel models#------------------------

#DNK#-------------------------------------------

data_dnk <- data %>% ##dropping NAs
  drop_na(exp,gender_k,age_k_c,edu_k_bin,dnk,dnk_k)

nt_1 = glmer(dnk ~ 1+(1|inter_id)
             , data = data_dnk,family = binomial, control = glmerControl(optimizer = "bobyqa"),
             nAGQ = 10, weights = weight)

summary(nt_1)


nt_2 = glmer(dnk ~ gender + age_c + edu_cat_f +settlement_f+(1|inter_id)
             , data = data_dnk,family = binomial, control = glmerControl(optimizer = "bobyqa"),
             nAGQ = 10, weights = weight)

summary(nt_2)


nt_3 = glmer(dnk ~ gender + age_c + edu_cat_f +settlement_f+exp+
               gender_k+age_k_c+edu_k_bin+dnk_k+(1|inter_id)
             , data = data_dnk,family = binomial, control = glmerControl(optimizer = "bobyqa"),
             nAGQ = 10, weights = weight)

summary(nt_3)


#REF#-------------------------------------------

data_ref <- data %>% ##dropping NAs
  drop_na(exp,gender_k,age_k_c,edu_k_bin,ref,ref_k)

ref_1 = glmer(ref ~ 1+(1|inter_id)
              , data = data_ref,family = binomial, control = glmerControl(optimizer = "bobyqa"),
              nAGQ = 10, weights = weight)

summary(ref_1)


ref_2 = glmer(ref ~ gender + age_c + edu_cat_f +settlement_f+(1|inter_id)
              , data = data_ref,family = binomial, control = glmerControl(optimizer = "bobyqa"),
              nAGQ = 10, weights = weight)

summary(ref_2)


anova(ref_1,ref_2)

ref_3 = glmer(ref ~ gender + age_c + edu_cat_f +settlement_f+exp+
                gender_k+age_k_c+edu_k_bin+ref_k+(1|inter_id)
              , data = data_ref,family = binomial, control = glmerControl(optimizer = "bobyqa"),
              nAGQ = 10, weights = weight)

summary(ref_3)


#Notvote#-------------------------------------------

data_notvote <- data %>%
  drop_na(exp,gender_k,age_k_c,edu_k_bin,notvote,notvote_k)

notvote_1 = glmer(notvote ~ 1+(1|inter_id)
                   , data = data_notvote,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                   nAGQ = 10, weights = weight)

summary(notvote_1)


notvote_2 = glmer(notvote ~ gender + age_c + edu_cat_f +settlement_f+(1|inter_id)
                   , data = data_notvote,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                   nAGQ = 10, weights = weight)

summary(notvote_2)


anova(notvote_1,notvote_2)

notvote_3 = glmer(notvote ~ gender + age_c + edu_cat_f +settlement_f+exp+
                     gender_k+age_k_c+edu_k_bin+notvote_k+(1|inter_id)
                   , data = data_notvote,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                   nAGQ = 10, weights = weight)

summary(notvote_3)


#left_DNK#-------------------------------------------

data_left_dk <- data %>%
  drop_na(exp,gender_k,age_k_c,edu_k_bin,P12_nt,P12_ntk)

left_nt_1 = glmer(P12_nt ~ 1+(1|inter_id)
                  , data = data_left_dk,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                  nAGQ = 10, weights = weight)

summary(left_nt_1)

left_nt_2 = glmer(P12_nt ~ gender + age_c + edu_cat_f +settlement_f+(1|inter_id)
                  , data = data_left_dk,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                  nAGQ = 10, weights = weight)

summary(left_nt_2)

anova(left_nt_1,left_nt_2)

left_nt_3 = glmer(P12_nt ~ gender + age_c + edu_cat_f +settlement_f+exp+
                    gender_k+age_k_c+edu_k_bin+P12_ntk+(1|inter_id)
                  , data = data_left_dk,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                  nAGQ = 10, weights = weight)

summary(left_nt_3)


#liberal_REF#-------------------------------------------

data_left_ref <- data %>%
  drop_na(exp,gender_k,age_k_c,edu_k_bin,P12_ref,P12_refk)

left_ref_1 = glmer(P12_ref ~ 1+(1|inter_id)
                   , data = data_left_ref,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                   nAGQ = 10, weights = weight)

summary(left_ref_1)

left_ref_2 = glmer(P12_ref ~ gender + age_c + edu_cat_f +settlement_f+(1|inter_id)
                   , data = data_left_ref,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                   nAGQ = 10, weights = weight)

summary(left_ref_2)

anova(left_ref_1,left_ref_2)

left_ref_3 = glmer(P12_ref ~ gender + age_c + edu_cat_f +settlement_f+exp+
                     gender_k+age_k_c+edu_k_bin+P12_refk+(1|inter_id)
                   , data = data_left_ref,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                   nAGQ = 10, weights = weight)

summary(left_ref_3)

#liberal_DNK#-------------------------------------------

data_liberal_dk <- data %>%
  drop_na(exp,gender_k,age_k_c,edu_k_bin,P11_nt,P11_ntk)

liberal_nt_1 = glmer(P11_nt ~ 1+(1|inter_id)
                     , data = data_liberal_dk,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                     nAGQ = 10, weights = weight)

summary(liberal_nt_1)

liberal_nt_2 = glmer(P11_nt ~ gender + age_c + edu_cat_f +settlement_f+(1|inter_id)
                     , data = data_liberal_dk,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                     nAGQ = 10, weights = weight)

summary(liberal_nt_2)

anova(liberal_nt_1,liberal_nt_2)

liberal_nt_3 = glmer(P11_nt ~ gender + age_c + edu_cat_f +settlement_f+exp+
                       gender_k+age_k_c+edu_k_bin+P11_ntk+(1|inter_id)
                     , data = data_liberal_dk,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                     nAGQ = 10, weights = weight)

summary(liberal_nt_3)


#liberal_REF#-------------------------------------------


data_liberal_ref <- data %>%
  drop_na(exp,gender_k,age_k_c,edu_k_bin,P11_ref,P11_refk)

liberal_ref_1 = glmer(P11_ref ~ 1+(1|inter_id)
                      , data = data_liberal_ref,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                      nAGQ = 10, weights = weight)

summary(liberal_ref_1)

liberal_ref_2 = glmer(P11_ref ~ gender + age_c + edu_cat_f +settlement_f+(1|inter_id)
                      , data = data_liberal_ref,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                      nAGQ = 10, weights = weight)

summary(liberal_ref_2)

anova(liberal_ref_1,liberal_ref_2)

liberal_ref_3 = glmer(P11_ref ~ gender + age_c + edu_cat_f +settlement_f+exp+
                        gender_k+age_k_c+edu_k_bin+P11_refk+(1|inter_id)
                      , data = data_liberal_ref,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                      nAGQ = 10, weights = weight)

summary(liberal_ref_3)


#Fidesz#-------------------------------------------


data_fidesz <- data %>%
  drop_na(exp,gender_k,age_k_c,edu_k_bin,fidesz,fidesz_k)


fidesz_1 = glmer(fidesz ~ 1+(1|inter_id)
               , data = data_fidesz,family = binomial, control = glmerControl(optimizer = "bobyqa"),
               nAGQ = 10, weights = weight)

summary(fidesz_1)


fidesz_2 = glmer(fidesz ~ gender + age_c + edu_cat_f +settlement_f+(1|inter_id)
                 , data = data_fidesz,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                 nAGQ = 10, weights = weight)

summary(fidesz_2)

anova(fidesz_1,fidesz_2)

fidesz_3 = glmer(fidesz ~ gender + age_c + edu_cat_f +settlement_f+exp+
             gender_k+age_k_c+edu_k_bin+fidesz_k+(1|inter_id)
           , data = data_fidesz,family = binomial, control = glmerControl(optimizer = "bobyqa"),
           nAGQ = 10, weights = weight)

summary(fidesz_3)

#DK#-------------------------------------------

data_dk <- data %>%
  drop_na(exp,gender_k,age_k_c,edu_k_bin,dk,dk_k)

dk_1 = glmer(dk ~ 1+(1|inter_id)
                 , data = data_dk,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                 nAGQ = 10, weights = weight)

summary(dk_1)


dk_2 = glmer(dk ~ gender + age_c + edu_cat_f +settlement_f+(1|inter_id)
                 , data = data_dk,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                 nAGQ = 10, weights = weight)

summary(dk_2)

anova(dk_1,dk_2)

dk_3 = glmer(dk ~ gender + age_c + edu_cat_f +settlement_f+exp+
                   gender_k+age_k_c+edu_k_bin+dk_k+(1|inter_id)
                 , data = data_dk,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                 nAGQ = 10, weights = weight)

summary(dk_3)


#Jobbik#-----------------------


data_jobbik <- data %>%
  drop_na(exp,gender_k,age_k_c,edu_k_bin,jobbik,jobbik_k)

jobbik_1 = glmer(jobbik ~ 1+(1|inter_id)
             , data = data_jobbik,family = binomial, control = glmerControl(optimizer = "bobyqa"),
             nAGQ = 10, weights = weight)

summary(jobbik_1)


jobbik_2 = glmer(jobbik ~ gender + age_c + edu_cat_f +settlement_f+(1|inter_id)
             , data = data_jobbik,family = binomial, control = glmerControl(optimizer = "bobyqa"),
             nAGQ = 10, weights = weight)

summary(jobbik_2)


jobbik_3 = glmer(jobbik ~ gender + age_c + edu_cat_f +settlement_f+exp+
               gender_k+age_k_c+edu_k_bin+jobbik_k+(1|inter_id)
             , data = data_jobbik,family = binomial, control = glmerControl(optimizer = "bobyqa"),
             nAGQ = 10, weights = weight)

summary(jobbik_3)


#MSZP#-------------------------------------------


data_mszp <- data %>%
  drop_na(exp,gender_k,age_k_c,edu_k_bin,mszp,mszp_k)

mszp_1 = glmer(mszp ~ 1+(1|inter_id)
                 , data = data_mszp,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                 nAGQ = 10, weights = weight)

summary(mszp_1)


mszp_2 = glmer(mszp ~ gender + age_c + edu_cat_f +settlement_f+(1|inter_id)
                 , data = data_mszp,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                 nAGQ = 10, weights = weight)

summary(mszp_2)

anova(mszp_1,mszp_2)

mszp_3 = glmer(mszp ~ gender + age_c + edu_cat_f +settlement_f+exp+
                   gender_k+age_k_c+edu_k_bin+mszp_k+(1|inter_id)
                 , data = data_mszp,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                 nAGQ = 10, weights = weight)

summary(mszp_3)


#Momentum#-------------------------------------------

data_mom <- data %>%
  drop_na(exp,gender_k,age_k_c,edu_k_bin,mom,mom_k)

mom_1 = glmer(mom ~ 1+(1|inter_id)
                 , data = data_mom,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                 nAGQ = 10, weights = weight)

summary(mom_1)


mom_2 = glmer(mom ~ gender + age_c + edu_cat_f +settlement_f+(1|inter_id)
                 , data = data_mom,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                 nAGQ = 10, weights = weight)

summary(mom_2)

anova(mom_1,mom_2)

mom_3 = glmer(mom ~ gender + age_c + edu_cat_f +settlement_f+exp+
                   gender_k+age_k_c+edu_k_bin+mom_k+(1|inter_id)
                 , data = data_mom,family = binomial, control = glmerControl(optimizer = "bobyqa"),
                 nAGQ = 10, weights = weight)

summary(mom_3)


#liberal#-------------------------------------------

data_liberal <- data %>%
  drop_na(exp,gender_k,age_k_c,edu_k_bin,P11,P11_k)

liberal_1 = lmer(P11 ~ 1+(1|inter_id)
                 , data = data_liberal,REML = FALSE,
                 control=lmerControl(optimizer="optimx",
                                     optCtrl=list(method='nlminb')),weights = weight)
summary(liberal_1)


liberal_2 = lmer(P11 ~ gender + age_c + edu_cat_f +settlement_f+
                   (1|inter_id)
                 , data = data_liberal,REML = FALSE,
                 control=lmerControl(optimizer="optimx",
                                     optCtrl=list(method='nlminb')),weights = weight,na.action = na.exclude)
summary(liberal_2)

anova(liberal_1,liberal_2)


liberal_3 = lmer(P11 ~ gender + age_c + edu_cat_f +settlement_f+exp+
                   gender_k+age_k_c+edu_k_bin+P11_k_c+(1|inter_id)
                 , data = data_liberal,REML = FALSE,
                 control=lmerControl(optimizer="optimx",
                                     optCtrl=list(method='nlminb')),weights = weight,na.action = na.exclude)
summary(liberal_3)


#left-right#-------------------------------------------

data_left <- data %>%
  drop_na(exp,gender_k,age_k_c,edu_k_bin,P12,P12_k)

left_1 = lmer(P12 ~ 1+(1|inter_id)
                 , data = data_left,REML = FALSE,
                 control=lmerControl(optimizer="optimx",
                                     optCtrl=list(method='nlminb')),weights = weight)
summary(left_1)



left_2 = lmer(P12 ~ gender + age_c + edu_cat_f +settlement_f+
                   (1|inter_id)
                 , data = data_left,REML = FALSE,
                 control=lmerControl(optimizer="optimx",
                                     optCtrl=list(method='nlminb')),weights = weight)
summary(left_2)


left_3 = lmer(P12 ~ gender + age_c + edu_cat_f +settlement_f+(exp)+
                   gender_k+age_k_c+edu_k_bin+P12_k_c+(1|inter_id)
                 , data = data_left,REML = FALSE,
                 control=lmerControl(optimizer="optimx",
                                     optCtrl=list(method='nlminb')),weights = weight)
summary(left_3)

anova(left_2,left_3)

#plotting-------------------------

#fre distributions-----------------------

#Party affiliation---------------------

vote_1 <- plot_frq(data$vote_1,expand.grid = TRUE,weight.by=data$weight,
                   wrap.title = 100, geom.colors = "grey",axis.title="",
                   axis.labels=c("Fidesz-KDNP","DK","MSZP-P","LMP","Jobbik",
                                 "Momentum M.","OHM","Other","Do not know","Refused","Would not vote"))+theme_blank()+
  ggtitle("If elections were held this Sunday for which party would you vote for?\nRespondents (n=11185)")+
  theme(plot.title = element_text(hjust=0.5),text = element_text(size=10),axis.text = element_text(size = 10))

vote_1

ggsave(vote_1,file="vote.png",dpi=600)

#creating aggregated data of interviewer responses

agg_inter <- aggregate(list(v1=data$vote_1_k,v2=data$P11_k,v3=data$P12_k),by=list(ID = data$inter_id), mean)
agg_inter

vote_1k <- plot_frq(agg_inter$v1,expand.grid = TRUE,
                   wrap.title = 100, geom.colors = "grey",axis.title="",
                   axis.labels=c("Fidesz-KDNP","DK","MSZP-P","LMP","Jobbik",
                                 "Momentum M.","OHM","Other","Do not know","Refused","Would not vote"))+theme_blank()+
  ggtitle("If elections were held this Sunday for which party would you vote for?\nInterviewers (n=333)")+
  theme(plot.title = element_text(hjust=0.5),text = element_text(size=10),axis.text = element_text(size = 10))

vote_1k

ggsave(vote_1k,file="vote_k.png",dpi=600)

#liberal-conservative--------------------

liberal <- plot_frq(data$P11,expand.grid = TRUE,weight.by=data$weight,
                    geom.colors = "grey",axis.title="",
                    axis.labels=c("Extremely liberal","Liberal","Slightly liberal","Moderate, middle of the road",
"Slightly conservative","Conservative","Extremely conservative","Do not know","Refused"))+theme_blank()+
  ggtitle("We hear a lot of talk these days about liberals and conservatives. Here is a seven-point scale on \nwhich the political views that people might hold are arranged from
extremely liberal?point 1?to extremely conservative?point 7. \nWhere would you place yourself on this scale?\nRespondents (n=11825)")+
  theme(plot.title = element_text(hjust=0.5),text = element_text(size=10),axis.text = element_text(size = 9))

liberal

ggsave(liberal,file="liberal.png",w=9.05, h=4,dpi=600)

liberal_k <- plot_frq(agg_inter$v2,expand.grid = TRUE, 
                    geom.colors = "grey",axis.title="",
                    axis.labels=c("Extremely liberal","Liberal","Slightly liberal","Moderate, middle of the road",
                                  "Slightly conservative","Conservative","Extremely conservative","Do not know","Refused"))+theme_blank()+
  ggtitle("We hear a lot of talk these days about liberals and conservatives. Here is a seven-point scale on \nwhich the political views that people might hold are arranged from
extremely liberal?point 1?to extremely conservative?point 7. \nWhere would you place yourself on this scale? \nInterviewers (n=333)")+
  theme(plot.title = element_text(hjust=0.5),text = element_text(size=10),axis.text = element_text(size = 9))

liberal_k

ggsave(liberal_k,file="liberal_k.png",w=9.05, h=4.2,dpi=600)

#left-right-------------------

left <- plot_frq(data$P12,expand.grid = TRUE,weight.by=data$weight,
                    geom.colors = "grey",axis.title="",
                    axis.labels=c("Extremely left","Left","Slightly left","Moderate, middle of the road",
                                  "Slightly right","Right","Extremely right","Do not know","Refused"))+theme_blank()+
  ggtitle("We hear a lot of talk these days about left and right Here is a seven-point scale on \nwhich the political views that people might hold are arranged from
extremely left 1?to extremely right 7. \nWhere would you place yourself on this scale?\nRespondents (n=11825)")+
  theme(plot.title = element_text(hjust=0.5),text = element_text(size=10),axis.text = element_text(size = 9))

left

ggsave(left,file="left.png",w=9.05, h=4,dpi=600)

left_k <- plot_frq(data$P12_k,expand.grid = TRUE, 
                      geom.colors = "grey",axis.title="",
                   axis.labels=c("Extremely left","Left","Slightly left","Moderate, middle of the road",
                                 "Slightly right","Right","Extremely right","Do not know","Refused"))+theme_blank()+
  ggtitle("We hear a lot of talk these days about left and right Here is a seven-point scale on \nwhich the political views that people might hold are arranged from
extremely left 1?to extremely right 7. \nWhere would you place yourself on this scale?\nInterviewers (n=333)")+
  theme(plot.title = element_text(hjust=0.5),text = element_text(size=10),axis.text = element_text(size = 9))

left_k

ggsave(left_k,file="left_k.png",w=9.05, h=4.2,dpi=600)

#AMEs------------------------

##DK_vote choice
  
nt_3_margins <- margins(nt_3)
summary(nt_3_margins)
margins = summary(nt_3_margins)
margins <- as.data.frame(margins)

write.csv(file="margins.csv",margins)

margins <- margins %>%
  mutate(p_value = case_when(p < 0.05 ~ 1,p > 0.05 ~ 0))

margins


dnk_party <-  ggplot(margins,aes(factor, AME,color=as.factor(p_value))) +
  geom_point(size=3)+
  geom_errorbar(aes(x = factor, ymin = lower, ymax = upper),width=0) +
  geom_hline(yintercept = 0,color="grey75",linetype="dashed") +
  theme_minimal() +
  scale_color_manual(values=c("grey80","grey40")) +
  ggtitle("DNK - vote choice") +
  theme(plot.title = element_text(hjust=0.5),
        text=element_text(family="Times New Roman",size=18),
        legend.position = "none",
        panel.grid.major.y = element_blank(),
        panel.grid.minor.y=element_blank())+
  scale_y_continuous(name="AME")+
  scale_x_discrete(
                   name="",breaks=c("dnk_k",
                                    "edu_k_bin1",
                                    "age_k_c",
                                    "gender_k",
                                    "exp"),
                   labels=c("DNK - int.","Diploma - int","Age - int.","Gender (Female) - int.",
                            "Experience - int."),
                   limits=c("dnk_k",
                            "edu_k_bin1",
                            "age_k_c",
                            "gender_k",
                            "exp"))+coord_flip()
dnk_party

ggsave(dnk_party,file="dnk_party.png",w=6, h=5)

##REF_vote choice

ref_3_margins <- margins(ref_3)
summary(ref_3_margins)
margins = summary(ref_3_margins)
margins <- as.data.frame(margins)
write.csv(file="margins2.csv",margins)

margins <- margins %>%
  mutate(p_value = case_when(p < 0.05 ~ 1,p > 0.05 ~ 0))

ref_party <-  ggplot(margins,aes(factor, AME,color=as.factor(p_value))) +
  geom_point(size=3)+
  geom_errorbar(aes(x = factor, ymin = lower, ymax = upper),width=0) +
  geom_hline(yintercept = 0,color="grey75",linetype="dashed") +
  theme_minimal() +
  scale_color_manual(values=c("grey80","grey40")) +
  ggtitle("Refused - vote choice") +
  theme(plot.title = element_text(hjust=0.5),
        text=element_text(family="Times New Roman",size=18),
        legend.position = "none",
        panel.grid.major.y = element_blank(),
        panel.grid.minor.y=element_blank())+
  scale_y_continuous(name="AME")+
  scale_x_discrete(
                   name="",breaks=c("ref_k",
                                    "edu_k_bin1",
                                    "age_k_c",
                                    "gender_k",
                                    "exp"),
                   labels=c("Refused - int.","Diploma - int","Age - int.","Gender (Female) - int.",
                            "Experience - int."),
                   limits=c("ref_k",
                            "edu_k_bin1",
                            "age_k_c",
                            "gender_k",
                            "exp"))+coord_flip()
ref_party
ggsave(ref_party,file="ref_party.png",w=6, h=5)




##Not vote vote choice

notvote_3_margins <- margins(notvote_3)
summary(notvote_3_margins)
margins = summary(notvote_3_margins)
margins <- as.data.frame(margins)
write.csv(file="margins3.csv",margins)


margins <- margins %>%
  mutate(p_value = case_when(p < 0.05 ~ 1,p > 0.05 ~ 0))

margins

notvote_party <-  ggplot(margins,aes(factor, AME,color=as.factor(p_value))) +
  geom_point(size=3)+
  geom_errorbar(aes(x = factor, ymin = lower, ymax = upper),width=0) +
  geom_hline(yintercept = 0,color="grey75",linetype="dashed") +
  theme_minimal() +
  scale_color_manual(values=c("grey80","grey40")) +
  ggtitle("Would not vote - vote choice") +
  theme(plot.title = element_text(hjust=0.5),
        text=element_text(family="Times New Roman",size=18),
        legend.position = "none",
        panel.grid.major.y = element_blank(),
        panel.grid.minor.y=element_blank())+
  scale_y_continuous(name="AME")+
  scale_x_discrete(
                   name="",breaks=c("notvote_k",
                                    "edu_k_bin1",
                                    "age_k_c",
                                    "gender_k",
                                    "exp"),
                   labels=c("Would not vote - int.","Diploma - int","Age - int.","Gender (Female) - int.",
                            "Experience - int."),
                   limits=c("notvote_k",
                            "edu_k_bin1",
                            "age_k_c",
                            "gender_k",
                            "exp"))+coord_flip()
notvote_party
ggsave(notvote_party,file="notvote_party.png",w=6, h=5)



##Liberal DK

liberal_nt3_margins <- margins(liberal_nt_3)
summary(liberal_nt3_margins)
margins = summary(liberal_nt3_margins)
margins <- as.data.frame(margins)
write.csv(file="margins4.csv",margins)


margins <- margins %>%
  mutate(p_value = case_when(p < 0.05 ~ 1,p > 0.05 ~ 0))

liberal_nt <-  ggplot(margins,aes(factor, AME,color=as.factor(p_value))) +
  geom_point(size=3)+
  geom_errorbar(aes(x = factor, ymin = lower, ymax = upper),width=0) +
  geom_hline(yintercept = 0,color="grey75",linetype="dashed") +
  theme_minimal() +
  scale_color_manual(values=c("grey80","grey40")) +
  ggtitle("DNK - liberal-conservative") +
  theme(plot.title = element_text(hjust=0.5),
        text=element_text(family="Times New Roman",size=18),
        legend.position = "none",
        panel.grid.major.y = element_blank(),
        panel.grid.minor.y=element_blank())+
  scale_y_continuous(name="AME")+
  scale_x_discrete(
                   name="",breaks=c("P11_ntk",
                                    "edu_k_bin1",
                                    "age_k_c",
                                    "gender_k",
                                    "exp"),
                   labels=c("DNK - int.","Diploma - int","Age - int.","Gender (Female) - int.",
                            "Experience - int."),
                   limits=c("P11_ntk",
                            "edu_k_bin1",
                            "age_k_c",
                            "gender_k",
                            "exp"))+coord_flip()
liberal_nt
ggsave(liberal_nt,file="liberal_nt.png",w=6, h=5)


##Liberal REF

liberal_ref3_margins <- margins(liberal_ref_3)
summary(liberal_ref3_margins)
margins = summary(liberal_ref3_margins)
margins <- as.data.frame(margins)
write.csv(file="margins5.csv",margins)


margins <- margins %>%
  mutate(p_value = case_when(p < 0.05 ~ 1,p > 0.05 ~ 0))

liberal_ref <-  ggplot(margins,aes(factor, AME,color=as.factor(p_value))) +
  geom_point(size=3)+
  geom_errorbar(aes(x = factor, ymin = lower, ymax = upper),width=0) +
  geom_hline(yintercept = 0,color="grey75",linetype="dashed") +
  theme_minimal() +
  scale_color_manual(values=c("grey80","grey40")) +
  ggtitle("Refused - liberal-conservative") +
  theme(plot.title = element_text(hjust=0.5),
        text=element_text(family="Times New Roman",size=18),
        legend.position = "none",
        panel.grid.major.y = element_blank(),
        panel.grid.minor.y=element_blank())+
  scale_y_continuous(name="AME")+
  scale_x_discrete(
                   name="",breaks=c("P11_refk",
                                    "edu_k_bin1",
                                    "age_k_c",
                                    "gender_k",
                                    "exp"),
                   labels=c("Refused - int.","Diploma - int","Age - int.","Gender (Female) - int.",
                            "Experience - int."),
                   limits=c("P11_refk",
                            "edu_k_bin1",
                            "age_k_c",
                            "gender_k",
                            "exp"))+coord_flip()
liberal_ref
ggsave(liberal_ref,file="liberal_ref.png",w=6, h=5)



##Left DK

left_nt3_margins <- margins(left_nt_3)
summary(left_nt3_margins)
margins = summary(left_nt3_margins)
margins <- as.data.frame(margins)
write.csv(file="margins6.csv",margins)


margins <- margins %>%
  mutate(p_value = case_when(p < 0.05 ~ 1,p > 0.05 ~ 0))

left_nt <-  ggplot(margins,aes(factor, AME,color=as.factor(p_value))) +
  geom_point(size=3)+
  geom_errorbar(aes(x = factor, ymin = lower, ymax = upper),width=0) +
  geom_hline(yintercept = 0,color="grey75",linetype="dashed") +
  theme_minimal() +
  scale_color_manual(values=c("grey80","grey40")) +
  ggtitle("DNK - left-right") +
  theme(plot.title = element_text(hjust=0.5),
        text=element_text(family="Times New Roman",size=18),
        legend.position = "none",
        panel.grid.major.y = element_blank(),
        panel.grid.minor.y=element_blank())+
  scale_y_continuous(name="AME")+
  scale_x_discrete(
                   name="",breaks=c("P12_ntk",
                                    "edu_k_bin1",
                                    "age_k_c",
                                    "gender_k",
                                    "exp"),
                   labels=c("DNK - int.","Diploma - int","Age - int.","Gender (Female) - int.",
                            "Experience - int."),
                   limits=c("P12_ntk",
                            "edu_k_bin1",
                            "age_k_c",
                            "gender_k",
                            "exp"))+coord_flip()
left_nt
ggsave(left_nt,file="left_nt.png",w=6, h=5)


##Left REF

left_ref3_margins <- margins(left_ref_3)
summary(left_ref3_margins)
margins = summary(left_ref3_margins)
margins <- as.data.frame(margins)
write.csv(file="margins7.csv",margins)


margins <- margins %>%
  mutate(p_value = case_when(p < 0.05 ~ 1,p > 0.05 ~ 0))

left_ref <-  ggplot(margins,aes(factor, AME,color=as.factor(p_value))) +
  geom_point(size=3)+
  geom_errorbar(aes(x = factor, ymin = lower, ymax = upper),width=0) +
  geom_hline(yintercept = 0,color="grey75",linetype="dashed") +
  theme_minimal() +
  scale_color_manual(values=c("grey80","grey40")) +
  ggtitle("Refused - left-right") +
  theme(plot.title = element_text(hjust=0.5),
        text=element_text(family="Times New Roman",size=18),
        legend.position = "none",
        panel.grid.major.y = element_blank(),
        panel.grid.minor.y=element_blank())+
  scale_y_continuous(name="AME")+
  scale_x_discrete(
                   name="",breaks=c("P12_refk",
                                    "edu_k_bin1",
                                    "age_k_c",
                                    "gender_k",
                                    "exp"),
                   labels=c("Refused - int.","Diploma - int","Age - int.","Gender (Female) - int.",
                            "Experience - int."),
                   limits=c("P12_refk",
                            "edu_k_bin1",
                            "age_k_c",
                            "gender_k",
                            "exp"))+coord_flip()
left_ref
ggsave(left_ref,file="left_ref.png",w=6, h=5)

##Fidesz

fidesz_margins <- margins(fidesz_3)
summary(fidesz_margins)
margins = summary(fidesz_margins)
margins <- as.data.frame(margins)
margins
margins <- margins %>% 
  mutate(ame2=round(AME,digits=2),
         se2=round(SE,digits=2))

write.csv(file="margins1.csv",margins)

##DK

dk_margins <- margins(dk_3)
summary(dk_margins)
margins = summary(dk_margins)
margins <- as.data.frame(margins)
margins
margins <- margins %>% 
  mutate(ame2=round(AME,digits=2),
         se2=round(SE,digits=2))

write.csv(file="margins2.csv",margins)

##Jobbik

jobbik_margins <- margins(jobbik_3)
summary(jobbik_margins)
margins = summary(jobbik_margins)
margins <- as.data.frame(margins)
margins
margins <- margins %>% 
  mutate(ame2=round(AME,digits=2),
         se2=round(SE,digits=2))

write.csv(file="margins3.csv",margins)

##Mszp

mszp_margins <- margins(mszp_3)
summary(mszp_margins)
margins = summary(mszp_margins)
margins <- as.data.frame(margins)
margins
margins <- margins %>% 
  mutate(ame2=round(AME,digits=2),
         se2=round(SE,digits=2))

write.csv(file="margins4.csv",margins)

##Mom

mom_margins <- margins(mom_3)
summary(mom_margins)
margins = summary(mom_margins)
margins <- as.data.frame(margins)
margins
margins <- margins %>% 
  mutate(ame2=round(AME,digits=2),
         se2=round(SE,digits=2))

write.csv(file="margins5.csv",margins)
